const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

// Page-im管理列表
router.get('/im/imConversationList', async ctx => {
  let script = ['/im.list.js'];
  let modConfig = modules['im.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/im/imConversationList', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('会话管理', body, script, modConfig.template, ctx)
});

// Page-im会话列表
router.get('/im/imChat', async ctx => {
  let script = ['/im.chat.js'];
  let modConfig = modules['im.chat'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/im/imChat', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('会话列表', body, script, modConfig.template, ctx)
});

// Page-im会话详情
router.get('/im/imDetail', async ctx => {
  let script = ['/im.detail.js'];
  let modConfig = modules['im.detail'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(`/im/imDetail?id=${ctx.request.query.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('会话详情', body, script, modConfig.template, ctx)
});

// Page-im排队会话
router.get('/im/conversationLine', async ctx => {
  let script = ['/im.lineup.js'];
  let modConfig = modules['im.lineup'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/im/conversationLine', 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('排队会话', body, script, modConfig.template, ctx)
});

module.exports = router;